Skip to content

feat(python): add AgentOverlapAnalyzer — Python port of TypeScript analyzer#541

Open
nuthalapativarun wants to merge 1 commit into
2FastLabs:mainfrom
nuthalapativarun:feat/119-python-agent-overlap-analyzer
Open

feat(python): add AgentOverlapAnalyzer — Python port of TypeScript analyzer#541
nuthalapativarun wants to merge 1 commit into
2FastLabs:mainfrom
nuthalapativarun:feat/119-python-agent-overlap-analyzer

Conversation

@nuthalapativarun

Copy link
Copy Markdown

Issue Link (REQUIRED)

Fixes #119

Summary

Changes

Adds python/src/agent_squad/agent_overlap_analyzer.py — a pure-stdlib Python implementation of the existing TypeScript AgentOverlapAnalyzer.

Algorithm (matches the TypeScript reference):

  1. Tokenise and remove English stopwords from each agent description
  2. Build TF-IDF vectors (implemented without external dependencies)
  3. Compute pairwise cosine similarity
  4. Label pairs: High (>0.3), Medium (>0.1), Low (≤0.1)
  5. Derive a uniqueness score per agent (1 − mean similarity to others)

Public API mirrors the TypeScript class:

from agent_squad.agent_overlap_analyzer import AgentOverlapAnalyzer

analyzer = AgentOverlapAnalyzer({
    "flight_agent": {"name": "Flight Agent", "description": "..."},
    "hotel_agent":  {"name": "Hotel Agent",  "description": "..."},
})
result = analyzer.analyze_overlap()
# result.pairwise_overlap["flight_agent__hotel_agent"].potential_conflict
# → "Low" | "Medium" | "High"

Also adds python/src/tests/test_agent_overlap_analyzer.py (12 tests) covering: return type, pair count, key format, percentage bounds, conflict levels, uniqueness score count and bounds, high/low overlap cases, single-agent and empty-agent edge cases.

No new external dependencies — pure stdlib (math, re).

User experience

Before: Python users had no equivalent of the TypeScript overlap analyzer and had to write their own or use the TS version.

After: same analysis is available in Python with identical thresholds and output format.

Checklist

  • I have performed a self-review of this change
  • Changes have been tested
  • Changes are documented
  • I have linked this PR to an existing issue (required)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…alyzer

Adds python/src/agent_squad/agent_overlap_analyzer.py as a pure-stdlib
Python implementation of the TypeScript AgentOverlapAnalyzer. Uses the
same TF-IDF / cosine-similarity approach and identical conflict
thresholds (>0.3 → High, >0.1 → Medium, else Low). Includes 12 unit
tests covering pairwise overlap, uniqueness scores, conflict levels,
edge cases (0 or 1 agent), and identical / orthogonal descriptions.

Closes 2FastLabs#119
@cornelcroi

Copy link
Copy Markdown
Collaborator

Hey @nuthalapativarun, nice work on the Python port! The algorithm is clean, the tests cover the important cases, and no new dependencies is the right call. A few things to fix before this can merge:

Blockers:

  1. The docs page (cookbook/monitoring/agent-overlap.md) is still TypeScript-only — needs a Python example added. That's a hard requirement in this repo.

  2. The analyze_overlap() method uses bare print() calls. The TypeScript version goes through Logger, and the Python side has the same Logger utility available. Raw prints make it hard to use this as a library — callers can't silence or redirect the output. Can you swap those to use the project logger?

  3. CI hasn't run on this branch yet. Need to see it pass (3.11, 3.12, 3.13) before merging.

Smaller thing:

AgentOverlapAnalyzer isn't exported from __init__.py, unlike the TypeScript version which is in the barrel export. Either add it there or let me know if that's intentional.

One side note — the IDF formula you're using (log((n+1)/(df+1)) + 1) is sklearn's smooth variant, while the natural npm library uses standard IDF (log(n/df)). So the raw overlap percentages will be different between Python and TS for the same inputs, even though the thresholds and labels match. Not necessarily a problem, but worth calling out in the PR so people aren't surprised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Agent overlap analysis script for Python

2 participants